package com.cn.yugan.Utils.MyViews;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.cn.yugan.mobile.R;
import com.cn.yugan.Utils.MLog;


public class MyCommentBgView extends View {
	/** 背景颜色 */
	private int bgColor = 0xFFC0E7EF;
	/** 线颜色 */
	private int lineColor = 0xFF94D7E4;
	/** 半径 */
	private int edgesRadius;
	/** 多条边框的间距-也是padding值 */
	private int linePadding;
	/** 线宽 */
	private float lineWidth;

	/** 当前背景样式 */
	private int bgStyle = MyBgStyle.ONLY_ONE;
	/** 堆叠的层数 */
	private int tierNum = 0;
	/**
	 * 绘制时控制文本绘制的范围
	 */
	private Rect mBound;
	private Paint mPaint;

	public MyCommentBgView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public MyCommentBgView(Context context) {
		this(context, null);
	}

	/**
	 * 获得我自定义的样式属性
	 * 
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public MyCommentBgView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		MLog.d("读取样式?");
		/**
		 * 获得我们所定义的自定义样式属性
		 */
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.MyCommentBgView, defStyle, 0);
		int n = a.getIndexCount();
		for (int i = 0; i < n; i++) {
			int attr = a.getIndex(i);
			switch (attr) {
			case R.styleable.MyCommentBgView_bgColor:
				bgColor = a.getColor(attr, 0xC0E7EF);
				// bgColor = a.getColor(attr, 0XFFC0E7EF);
				break;
			case R.styleable.MyCommentBgView_lineColor:
				// 默认颜色设置为黑色
				lineColor = a.getColor(attr, 0x94D7E4);
				break;
			case R.styleable.MyCommentBgView_edgesRadius:
				// 默认设置为16dp，TypeValue也可以把dp转化为px
				edgesRadius = a.getDimensionPixelSize(attr, (int) TypedValue
						.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16,
								getResources().getDisplayMetrics()));
				break;
			case R.styleable.MyCommentBgView_linePadding:
				// 默认设置为16dp，TypeValue也可以把dp转化为px
				linePadding = a.getDimensionPixelSize(attr, (int) TypedValue
						.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16,
								getResources().getDisplayMetrics()));
				break;
			case R.styleable.MyCommentBgView_lineWidth:
				// 默认设置为16dp，TypeValue也可以把dp转化为px
				lineWidth = a.getDimensionPixelSize(attr, (int) TypedValue
						.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,
								getResources().getDisplayMetrics()));
				break;

			}

		}
		a.recycle();

		/**
		 * 获得绘制文本的宽和高
		 */
		mPaint = new Paint();
		// mPaint.setTextSize(mTitleTextSize);
		// // mPaint.setColor(mTitleTextColor);
		// mBound = new Rect();
		// mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		/** 绘制背景 */
		// mPaint.setStyle(Paint.Style.FILL);// 充满
		// mPaint.setColor(bgColor);
		// mPaint.setAntiAlias(true);// 设置画笔的锯齿效果
		// // 设置个新的长方形
		// RectF ovalBg = new RectF(0, 0, getMeasuredWidth(),
		// getMeasuredHeight()
		// + edgesRadius);
		// // 第二个参数是x半径，第三个参数是y半径
		// canvas.drawRoundRect(ovalBg, edgesRadius, edgesRadius, mPaint);
		drawBackgroup(canvas);

		/** 绘制边框 */
		drawFrames(canvas);
		// mPaint.setStyle(Paint.Style.STROKE);// 充满
		// mPaint.setColor(lineColor);
		// mPaint.setAntiAlias(true);// 设置画笔的锯齿效果
		// RectF ovalLine = null;
		// for (int i = 0; i < 5; i++) {
		// // 设置个新的长方形
		// ovalLine = new RectF(0 + linePadding * i, 0 + linePadding * i,
		// getMeasuredWidth() - linePadding * i, getMeasuredHeight()
		// - linePadding * i);
		// // 第二个参数是x半径，第三个参数是y半径
		// canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
		// }
	}

	private void drawFrames(Canvas canvas) {
		mPaint.setStyle(Paint.Style.STROKE);// 充满
		mPaint.setColor(lineColor);
		mPaint.setStrokeWidth(lineWidth);// 设置线宽
		mPaint.setAntiAlias(true);// 设置画笔的锯齿效果
		RectF ovalLine = null;
		switch (bgStyle) {
		case MyBgStyle.ONLY_ONE:
			/* 绘制圆角四边形边框 */
			if (tierNum == 1) {
				// 设置个新的长方形
				ovalLine = new RectF(0, 0, getMeasuredWidth(),
						getMeasuredHeight());
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			}
			break;
		case MyBgStyle.HEAD:
			/* 绘制上方两圆角,下方直角四边形 */

			for (int i = 0; i < (tierNum - 1); i++) {
				// 设置个新的长方形
				ovalLine = new RectF(lineWidth / 2 + linePadding * i, lineWidth
						/ 2 + linePadding * i, -lineWidth / 2
						+ getMeasuredWidth() - linePadding * i,
						getMeasuredHeight() + edgesRadius);
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			}
			// 设置个新的长方形
			ovalLine = new RectF(lineWidth / 2 + linePadding * (tierNum - 1),
					lineWidth / 2 + linePadding * (tierNum - 1), -lineWidth
							+ getMeasuredWidth() - linePadding * (tierNum - 1),
					getMeasuredHeight() - lineWidth);
			// 第二个参数是x半径，第三个参数是y半径
			canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			break;
		case MyBgStyle.MAX_HEAD:
			/* 绘制上方圆角,下方直角四边形 */
			for (int i = 0; i < (tierNum - 1); i++) {
				// 设置个新的长方形
				ovalLine = new RectF(lineWidth / 2 + linePadding * i, lineWidth
						/ 2 + linePadding * i, -lineWidth / 2
						+ getMeasuredWidth() - linePadding * i,
						getMeasuredHeight() + edgesRadius);
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			}
			// 设置个新的长方形
			ovalLine = new RectF(lineWidth / 2 + linePadding * (tierNum - 1),
					lineWidth / 2 + linePadding * (tierNum - 1), -lineWidth
							+ getMeasuredWidth() - linePadding * (tierNum - 1),
					getMeasuredHeight() + edgesRadius);
			// 第二个参数是x半径，第三个参数是y半径
			canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			canvas.drawLine(lineWidth / 2 + linePadding * (tierNum - 1),
					getMeasuredHeight() - lineWidth, -lineWidth
							+ getMeasuredWidth() - linePadding * (tierNum - 1),
					getMeasuredHeight() - lineWidth, mPaint);// 画线
			break;
		case MyBgStyle.MAX_CENTRE:
			/* 绘制直角四边形 */
			for (int i = 0; i < tierNum; i++) {
				// 设置个新的长方形
				ovalLine = new RectF(lineWidth / 2 + linePadding * i,
						0 - edgesRadius, -lineWidth / 2 + getMeasuredWidth()
								- linePadding * i, getMeasuredHeight()
								+ edgesRadius);
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			}
			canvas.drawLine(lineWidth / 2 + linePadding * (tierNum - 1),
					getMeasuredHeight() - lineWidth, -lineWidth
							+ getMeasuredWidth() - linePadding * (tierNum - 1),
					getMeasuredHeight() - lineWidth, mPaint);// 画线
			break;
		case MyBgStyle.CENTRE_END:
			// /* 绘制无上边框,下方圆角的四边形 */
			for (int i = 0; i < (tierNum - 1); i++) {
				// 设置个新的长方形
				ovalLine = new RectF(lineWidth / 2 + linePadding * i,
						0 - edgesRadius, -lineWidth / 2 + getMeasuredWidth()
								- linePadding * i, getMeasuredHeight()
								+ linePadding);
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			}
			// 设置个新的长方形
			ovalLine = new RectF(lineWidth / 2 + linePadding * (tierNum - 1),
					0 - edgesRadius, -lineWidth / 2 + getMeasuredWidth()
							- linePadding * (tierNum - 1), -lineWidth / 2
							+ getMeasuredHeight() - lineWidth / 2);
			// 第二个参数是x半径，第三个参数是y半径
			canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			// ovalLine = new RectF(0 + linePadding * (tierNum - 1),
			// 0 - edgesRadius, getMeasuredWidth() - linePadding
			// * (tierNum - 1), getMeasuredHeight() - linePadding
			// * (tierNum - 1));
			// // 第二个参数是x半径，第三个参数是y半径
			// canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			break;
		case MyBgStyle.END:
			/* 绘制上方直角,下方圆角的 */
			// 设置个新的长方形
			ovalLine = new RectF(lineWidth / 2 + linePadding * (tierNum - 1),
					0 - edgesRadius, -lineWidth / 2 + getMeasuredWidth()
							- linePadding * (tierNum - 1), getMeasuredHeight()
							- lineWidth / 2);
			// 第二个参数是x半径，第三个参数是y半径
			canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
			break;
		}
		// for (int i = 0; i < 5; i++) {
		// // 设置个新的长方形
		// ovalLine = new RectF(0 + linePadding * i, 0 + linePadding * i,
		// getMeasuredWidth() - linePadding * i, getMeasuredHeight()
		// - linePadding * i);
		// // 第二个参数是x半径，第三个参数是y半径
		// canvas.drawRoundRect(ovalLine, edgesRadius, edgesRadius, mPaint);
		// }
	}

	private void drawBackgroup(Canvas canvas) {
		if (tierNum == 0) {
			return;
		} else {
			mPaint.setStyle(Paint.Style.FILL);// 充满
			mPaint.setColor(bgColor);
			mPaint.setAntiAlias(true);// 设置画笔的锯齿效果
			RectF ovalBg = null;
			switch (bgStyle) {
			case MyBgStyle.ONLY_ONE:
				/* 绘制圆角四边形 */
				// 设置个新的长方形
				ovalBg = new RectF(0, 0, getMeasuredWidth(),
						getMeasuredHeight());
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRoundRect(ovalBg, edgesRadius, edgesRadius, mPaint);
				break;
			case MyBgStyle.HEAD:
				/* 绘制上方两圆角,下方直角四边形 */
				// 设置个新的长方形
				ovalBg = new RectF(0, 0, getMeasuredWidth(),
						getMeasuredHeight() + edgesRadius);
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRoundRect(ovalBg, edgesRadius, edgesRadius, mPaint);
				break;
			case MyBgStyle.MAX_HEAD:
				/* 绘制上方两圆角,下方直角四边形 */
				// 设置个新的长方形
				ovalBg = new RectF(0, 0, getMeasuredWidth(),
						getMeasuredHeight() + edgesRadius);
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRoundRect(ovalBg, edgesRadius, edgesRadius, mPaint);
				break;
			case MyBgStyle.MAX_CENTRE:
				/* 绘制直角四边形 */
				// 设置个新的长方形
				ovalBg = new RectF(0, 0, getMeasuredWidth(),
						getMeasuredHeight());
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRect(ovalBg, mPaint);
				break;
			case MyBgStyle.CENTRE_END:
				/* 绘制直角四边形 */
				// 设置个新的长方形
				ovalBg = new RectF(0, 0, getMeasuredWidth(),
						getMeasuredHeight());
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRect(ovalBg, mPaint);
				break;
			case MyBgStyle.END:
				/* 绘制上方两圆角,下方直角四边形 */
				// 设置个新的长方形
				ovalBg = new RectF(0, 0 - edgesRadius, getMeasuredWidth(),
						getMeasuredHeight());
				// 第二个参数是x半径，第三个参数是y半径
				canvas.drawRoundRect(ovalBg, edgesRadius, edgesRadius, mPaint);
				
				
				break;
			}
		}
	}

	/** 当前背景样式 */
	public void setBgStyle(int bgStyle) {
		this.bgStyle = bgStyle;
		invalidate();
	}

	/** 堆叠的层数 */
	public void setTierNum(int tierNum) {
		this.tierNum = tierNum;
		invalidate();
	}

	/** 当前背景样式 */
	public int getBgStyle() {
		return bgStyle;
	}

	/** 堆叠的层数 */
	public int getTierNum() {
		return tierNum;
	}

	public static class MyBgStyle {
		/** 唯一一层 */
		public static final int ONLY_ONE = 0x000;
		/** 5层堆叠内的head */
		public static final int HEAD = 0x100;
		/** 5层堆叠以上的head */
		public static final int MAX_HEAD = 0x900;
		/** 5层堆叠以上的center */
		public static final int MAX_CENTRE = 0x090;
		/** 5层堆叠内的end */
		public static final int CENTRE_END = 0x011;
		/** 第一层堆叠的end */
		public static final int END = 0x001;
	}
}
